﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.Web.Controllers
{
    public class LoginController : BaseController
    {
        public string turl = string.Empty;
        public IUser_login_logService _user_login_log { get; set; }
        public IUser_oauthService _user_oauth { get; set; }
        public IActionResult Index()
        {
            turl = "/Web/Usercenter/Index";
            string curl = Request.GetDisplayUrl();
            string referer = Request.Headers[HeaderNames.Referer].FirstOrDefault();
            if (curl != null && referer != null)
            {
                string currUrl = curl.ToLower(); //当前页面
                string refUrl = referer.ToLower(); //上一页面
                string regPath = "/Web/Register".ToLower(); //注册页面
                if (currUrl != refUrl && refUrl.IndexOf(regPath) == -1)
                {
                    turl = referer.ToString();
                }
            }
            Response.Cookies.Append(CMSKeys.COOKIE_URL_REFERRER, turl); //记住上一页面
            Users model = GetUserInfo();
            if (model != null)
            {
                //写入登录日志
                _user_login_log.Add(model.id, model.user_name, "自动登录",CMSRequest.GetClientUserIp(HttpContext));
                return Redirect(turl);
            }
            return View();
        }
        public IActionResult Oauth_login()
        {
            turl = Request.Cookies[CMSKeys.COOKIE_URL_REFERRER];
            string referer = Request.Headers[HeaderNames.Referer].FirstOrDefault();
            if (string.IsNullOrEmpty(referer))
                referer = "";
            if (string.IsNullOrEmpty(turl) || turl == referer.ToLower())
            {
                turl = "/Web/Usercenter/Index";
            }
            if (IsUserLogin())
            {
                //自动登录,跳转URL
                return Redirect(turl);
            }
            //检查是否已授权
            if (HttpContext.Session.GetString("oauth_name") == null || HttpContext.Session.GetString("oauth_access_token") == null || HttpContext.Session.GetString("oauth_openid") == null)
            {
                return ToError("登录失败，用户授权已过期，请重新登录！");
            }
            User_oauth oauthModel = _user_oauth.GetModel(HttpContext.Session.GetString("oauth_name"), HttpContext.Session.GetString("oauth_openid"));
            if (oauthModel != null)
            {
                //检查用户是否存在
                var model = _users.GetModel(oauthModel.user_name);
                if (model == null)
                {
                    return ToError("登录失败，授权用户不存在或已被删除！");
                }

                //记住登录状态，防止Session提前过期
                HttpContext.Session.Set(CMSKeys.SESSION_USER_INFO, ByteConvertHelper.Object2Bytes(model));
                HttpContext.Response.Cookies.Append(CMSKeys.COOKIE_USER_NAME_REMEMBER, model.user_name);
                HttpContext.Response.Cookies.Append(CMSKeys.COOKIE_USER_PWD_REMEMBER, model.password);
                //更新最新的Access Token
                oauthModel.oauth_access_token = HttpContext.Session.GetString("oauth_access_token");
                _user_oauth.Update(oauthModel);
                //自动登录,跳转URL
                return Redirect("/Web/Usercenter/Index");
            }
            return View();
        }
    }
}